<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- card title -->
        <div slot="header">
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ GongSi.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDepartment()">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <!-- card body -->
        <el-tree default-expand-all :data="treedata" :props="defaultProps">
          <template #default="{ data }">
            <el-row style="width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager||'--' }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data,1)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="addDepartment(data,2)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepartment(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <AddDept ref="ad" :old-shuju="oldData" :get-pid="val" :show-dialog="showDialogValue" @shuaxin="hGetDepartments()" @close-window="showDialogValue=$event" />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index.js'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      oldData: [],
      treedata: [],
      GongSi: {
        name: ''
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 点击子部门的当前数据
      val: null,
      showDialogValue: false
    }
  },
  created () {
    this.hGetDepartments()
  },
  methods: {
    // 添加子部门或父部门的函数
    addDepartment (val, type) {
      if (type === 2) {
        this.$refs.ad.backfillData(val.id)
      }
      this.val = val
      this.showDialogValue = true
    },
    // 获取用户数据
    async hGetDepartments () {
      const { depts, companyName } = await getDepartments()
      this.oldData = depts
      this.treedata = tranListToTreeData(depts)
      // this.treedata = depts
      this.GongSi.name = companyName
      // console.table(depts)
    },
    // 删除功能
    async delDepartment (val) {
      try {
        await this.$confirm(`你是否要删除${val.name}?`)
        // 判断如果是父部门不能直接删除
        if (val.children && val.children.length > 0) {
          return this.$message.error('不能直接删除父部门')
        }
        console.log(val.id)
        await delDepartments(val.id)
        this.hGetDepartments()
        this.$message.success('删除成功')
        console.log(val)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// sass样式无法覆盖饿了么是要加::v-deep
// less样式无法覆盖饿了么是要加/deep/
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
